<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" controls-position="right" :min="1" style="width: 100%;" placeholder="排序号"></el-input-number>
			</el-form-item>
			<el-form-item label="机构名称" prop="orgName">
				<el-input v-model="form.orgName" clearable placeholder="机构名称"></el-input>
			</el-form-item>
			<el-form-item label="机构管理员" prop="orgAdmin">
				<el-input v-model="form.orgAdmin" clearable placeholder="机构管理员"></el-input>
			</el-form-item>
			<el-form-item label="管理员密码" prop="password" v-if="mode=='add'">
				<el-input type="password" v-model="form.password" clearable show-password placeholder="管理员密码"></el-input>
			</el-form-item>
			<el-form-item label="备注说明" prop="remark">
				<el-input v-model="form.remark" maxlength="380" clearable type="textarea" placeholder="备注说明"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增机构',
					edit: '编辑机构',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					configId:'',
					sortNo:0,
					orgName: "",
					orgAdmin: "",
					password: "",
					remark: ""
				},
				//验证规则
				rules: {
					sortNo: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					orgName: [
						{required: true, message: '请输入机构名称'}
					],
					orgAdmin: [
						{required: true, message: '机构管理员不能为空'}
					],
					password: [
						{required: true, message: '机构管理员密码不能为空'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				if(this.mode=="add")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.system.sysOrgConfig.createOrgConfig.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}else if(this.mode=="edit")
				{
					this.$refs.dialogForm.validate(async (valid) => {
						if (valid) {
							this.isSaveing = true;
							var res = await this.$API.system.sysOrgConfig.update.post(this.form);
							this.isSaveing = false;
							if(res.code == 200){
								this.$emit('success', this.form, this.mode)
								this.visible = false;
								this.$message.success("操作成功")
							}else{
								this.$alert(res.message, "提示", {type: 'error'})
							}
						}
					})
				}
			},
			setData(data){
				console.log(data);
				this.form.configId = data.configId
				this.form.sortNo = data.sortNo
				this.form.orgName = data.orgName
				this.form.orgAdmin = data.orgAdmin
				this.form.password = data.password
				this.form.remark = data.remark
			}
		}
	}
</script>

<style>
</style>
